In [4]:
import tensorflow as tf
from tensorflow.contrib.layers import fully_connected
from tensorflow.examples.tutorials.mnist import input_data

import matplotlib.pyplot as plt 

% matplotlib inline

In [21]:
mnist = input_data.read_data_sets("tmp/mnist")


Extracting tmp/mnist/train-images-idx3-ubyte.gz
Extracting tmp/mnist/train-labels-idx1-ubyte.gz
Extracting tmp/mnist/t10k-images-idx3-ubyte.gz
Extracting tmp/mnist/t10k-labels-idx1-ubyte.gz

Model


In [22]:
n_steps = 28
n_inputs = 28

n_neurons = 150
n_outputs = 10

learning_rate = 1e-4

Graph


In [23]:
tf.reset_default_graph()

# inputs and labels
x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.int32, [None])

basic_cell = tf.contrib.rnn.BasicRNNCell(num_units = n_neurons)
outputs, states = tf.nn.dynamic_rnn(basic_cell, x, dtype=tf.float32)
logits = fully_connected(inputs=states, num_outputs=n_outputs, activation_fn=None)

xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
loss = tf.reduce_mean(xentropy)

optimiser = tf.train.AdamOptimizer(learning_rate)
training_op = optimiser.minimize(loss)

correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

init_op = tf.global_variables_initializer()

Session


In [35]:
n_epoch = 100
epoch_size = 500
batch_size = 120

with tf.Session() as sess:
    init_op.run()
    
    for epoch in range(n_epoch):
        for it in range(epoch_size):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            batch_x = batch_x.reshape((-1, n_steps, n_inputs))
        
            sess.run(training_op, feed_dict={x:batch_x, y:batch_y})
        
        acc_train = accuracy.eval(feed_dict={x:batch_x, y:batch_y})
        print(epoch, "Training Accuracy:", acc_train)
        
    x_test = mnist.test.images.reshape((-1, n_steps, n_inputs))
    y_test = mnist.test.labels

    acc_test = accuracy.eval(session=sess, feed_dict={x:x_test, y:y_test})
    print("Test Accuracy:", acc_test)


0 Training Accuracy: 0.858333
1 Training Accuracy: 0.85
2 Training Accuracy: 0.891667
3 Training Accuracy: 0.916667
4 Training Accuracy: 0.875
5 Training Accuracy: 0.958333
6 Training Accuracy: 0.958333
7 Training Accuracy: 0.95
8 Training Accuracy: 0.958333
9 Training Accuracy: 0.975
10 Training Accuracy: 0.933333
11 Training Accuracy: 0.966667
12 Training Accuracy: 0.958333
13 Training Accuracy: 0.975
14 Training Accuracy: 0.958333
15 Training Accuracy: 0.95
16 Training Accuracy: 0.975
17 Training Accuracy: 0.991667
18 Training Accuracy: 0.975
19 Training Accuracy: 0.975
20 Training Accuracy: 0.958333
21 Training Accuracy: 0.95
22 Training Accuracy: 0.991667
23 Training Accuracy: 0.983333
24 Training Accuracy: 0.966667
25 Training Accuracy: 1.0
26 Training Accuracy: 0.958333
27 Training Accuracy: 0.991667
28 Training Accuracy: 0.991667
29 Training Accuracy: 0.991667
30 Training Accuracy: 1.0
31 Training Accuracy: 0.991667
32 Training Accuracy: 0.966667
33 Training Accuracy: 0.975
34 Training Accuracy: 0.966667
35 Training Accuracy: 0.991667
36 Training Accuracy: 0.975
37 Training Accuracy: 0.991667
38 Training Accuracy: 0.966667
39 Training Accuracy: 0.983333
40 Training Accuracy: 0.983333
41 Training Accuracy: 0.983333
42 Training Accuracy: 0.983333
43 Training Accuracy: 0.966667
44 Training Accuracy: 1.0
45 Training Accuracy: 0.975
46 Training Accuracy: 0.983333
47 Training Accuracy: 0.983333
48 Training Accuracy: 0.991667
49 Training Accuracy: 0.983333
50 Training Accuracy: 0.983333
51 Training Accuracy: 0.991667
52 Training Accuracy: 0.991667
53 Training Accuracy: 0.983333
54 Training Accuracy: 1.0
55 Training Accuracy: 0.991667
56 Training Accuracy: 0.991667
57 Training Accuracy: 0.95
58 Training Accuracy: 1.0
59 Training Accuracy: 0.991667
60 Training Accuracy: 1.0
61 Training Accuracy: 1.0
62 Training Accuracy: 0.991667
63 Training Accuracy: 0.983333
64 Training Accuracy: 0.975
65 Training Accuracy: 1.0
66 Training Accuracy: 0.991667
67 Training Accuracy: 0.983333
68 Training Accuracy: 1.0
69 Training Accuracy: 0.991667
70 Training Accuracy: 0.983333
71 Training Accuracy: 1.0
72 Training Accuracy: 1.0
73 Training Accuracy: 1.0
74 Training Accuracy: 1.0
75 Training Accuracy: 1.0
76 Training Accuracy: 1.0
77 Training Accuracy: 0.975
78 Training Accuracy: 0.991667
79 Training Accuracy: 0.991667
80 Training Accuracy: 1.0
81 Training Accuracy: 0.991667
82 Training Accuracy: 1.0
83 Training Accuracy: 0.991667
84 Training Accuracy: 0.991667
85 Training Accuracy: 1.0
86 Training Accuracy: 1.0
87 Training Accuracy: 1.0
88 Training Accuracy: 1.0
89 Training Accuracy: 1.0
90 Training Accuracy: 1.0
91 Training Accuracy: 1.0
92 Training Accuracy: 0.991667
93 Training Accuracy: 1.0
94 Training Accuracy: 1.0
95 Training Accuracy: 1.0
96 Training Accuracy: 0.966667
97 Training Accuracy: 1.0
98 Training Accuracy: 1.0
99 Training Accuracy: 1.0
Test Accuracy: 0.978

In [36]:
mnist.train.num_examples


Out[36]:
55000

In [ ]: